<?php

namespace app\admin\model;

use phpspider\core\requests;
use phpspider\core\selector;
use think\Model;


class SpiderAiqiyi extends Model
{

    

    

    // 表名
    protected $name = 'spider_aiqiyi';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    //初始抓包
    public static function spider($id){
        $model = SpiderAiqiyi::where("id",$id)->find();
        if(empty($model)) return false;
        $url = $model["url"];
        $parent_id = $model["id"];
        if(empty($url)) return false;
        try{
            $content = requests::get($url);
            $album_id = selector::select($content, "/aid=(.*?)&/","regex");//获取albumId 用于获取热度
            $content_id = selector::select($content, "/tvid=(.*?)&/","regex");//获取content_id 用于获取评论数
            $gengxin = selector::select($content, "//p[@class='update-tip']");//获取更新时间，最新集
            $gengxin = strip_tags($gengxin);
            $gengxin = trim($gengxin);
            $gengxin_ar = explode(" ",$gengxin);
            $hot = self::hot($album_id);
            $comment_ar = self::comment_num($content_id);
            $comment_number = $comment_ar["comment_number"]??0;
            $barrage_number = $comment_ar["barrage_number"]??0;
            $update_data = [
                "hot"=>(string)$hot,
                "hot_number"=>$hot,
                "update_date"=>$gengxin_ar[0],
                "comment_number"=>$comment_number,
                "barrage_number"=>$barrage_number,
                "content_id"=>$content_id,
                "album_id"=>$album_id
            ];
            SpiderAiqiyi::update($update_data,['id'=>$parent_id]);

            self::collection($album_id,$parent_id);
        }catch (\Exception $e){
            throw $e;
        }
    }


    //获取热度
    public static function hot($albumId){
        $url = "https://pcw-api.iqiyi.com/video/video/hotplaytimes/".$albumId;
        $hot = 0;
        try{
            $content =  requests::get($url);
            $ar = json_decode($content,true);
            $hot = $ar['data'][0]['hot']??0;
            return $hot;
        }catch (\GuzzleHttp\Exception\RequestException $e){
            // throw $e;
//            $response = $e->getResponse();
//            $code=$response->getStatusCode();
//            $content = $response->getBody()->getContents();
//            p($code);
//            p($content);
//            p($e->getMessage());die;
            return $hot;
        }

        return $hot;
    }

    //获取评论数
    public static  function comment_num($content_id){
        $url ="https://sns-comment.iqiyi.com/v3/comment/get_comments.action?agent_type=118&business_type=17&content_id=".$content_id;
        try{
            $content = requests::get($url);
            $data = json_decode($content,true);
            //$pinglun_num =  selector::select($content, "/commentReplyCount\":(.*?),/","regex");// 评论数
            $comment_number = $data['data']["commentReplyCount"]??0;
            $barrage_number = $data['data']["totalCount"]??0;
            return ["comment_number"=>$comment_number,"barrage_number"=>$barrage_number];
        }catch (\Exception $e){

        }
        return ["comment_number"=>0,"barrage_number"=>0];
    }

    //集数
    public static function collection($album_id,$parent_id){
        $url = "https://pcw-api.iqiyi.com/albums/album/avlistinfo?aid=$album_id&page=1&size=500";
        try{
            $content = requests::get($url);
            $ar = json_decode($content,true);
            $epsodelist = $ar['data']['epsodelist']??[];
            $updateprevuelist = $ar['data']['updateprevuelist']??[];
            $arr = array_merge($epsodelist,$updateprevuelist);
            $list = [];
            foreach ($arr as $val){
                $list[]=[
                    "content_id"=>(string)$val['tvId'],
                    "collection"=>$val['name'],
                    "url"=>$val['playUrl'],
                    "parent_id"=>$parent_id
                ];
            }
            $model =  new \app\admin\model\SpiderAiqiyiCollection;
            $collection_list = $model->select();
            $content_ar = $collection_list?array_column($collection_list,"content_id"):[];
            foreach ($list as $list_val){
                if(!in_array($list_val['content_id'],$content_ar)){
                    $new_model = clone $model;
                    $new_model->save($list_val);
                }
            }
        }catch (\Exception $e){
            throw $e;
        }
    }
    

    







}
